<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Goods extends Model
{
    protected $guarded=['attrs','editormd-html-code','file'];

    protected $casts=[
        'category_id'=>'array',
        'pics'=>'array'
    ];

    /**
     * @param $goodsCategory
     * 获取分类id对应的name值
     */
    public function getCategory($goodsCategory){
//        dd($goodsCategory);
        $newArr=[];
        foreach ($goodsCategory as $value){
            $newArr[]=Category::where('id',$value)->value('category_name');
        }
        // 将获得的分类数组数据转换为json数据
        return json_encode($newArr,JSON_UNESCAPED_UNICODE);
    }

    public function getHuopin(){
        return $this->hasMany(Huopin::class,'goods_id','id');
    }

    public function isCollected(){
        return Collections::where('user_id',auth()->id())->where('goods_id',$this->id)->first();
    }
}
